<?php 
/**
 * 数据库适配器
 * @author Yuwei(yuwei1122@gmail.com)
 *
 */
class BluePhp_Library_Db_Adapter
{
	/**
	 * 构造函数
	 */
	 public function __construct($table)
	 {
	 	if(!$this->_drive)
		 	switch (DB_DRIVE)
		 	{
		 		case 'mysqli':
		 			$this->_drive = new BluePhp_Library_Db_Drives_Mysqli();
		 		break;
		 		case 'mysql':
		 		default:
		 			$this->_drive = new BluePhp_Library_Db_Drives_Mysql();
		 		break;
		 	}
		 $this->table($table);
	 }
	 
	 /**
	  * 驱动
	  * @var unknown_type
	  */
	 protected $_drive;
	 
	 /**
	  * 表名
	  * @var unknown_type
	  */
	 protected $_table;
	 
	 /**
	  * 条件
	  * @var unknown_type
	  */
	 protected $_where;
	 
	 /**
	  * 字段
	  * @var unknown_type
	  */
	 protected $_field = '*';
	 
	 /**
	  * 关联
	  * @var unknown_type
	  */
	 protected $_join;
	 
	 /**
	  * 分页
	  * @var unknown_type
	  */
	 protected $_limit;
	 
	 /**
	  * 分组
	  * @var unknown_type
	  */
	 protected $_group;
	 
	 /**
	  * 数据
	  * @var unknown_type
	  */
	 protected $_data;
	 
	 /**
	  * 排序
	  * @var unknown_type
	  */
	 protected $_order;
	 
	 /**
	  * 表名操作
	  * @param unknown_type $table
	  */
	 public function table($table)
	 {
	 	$this->_table = $table;
	 	return $this;
	 }
	 
	 /**
	  * 字段操作
	  * @param unknown_type $field
	  */
	 public function field($field)
	 {
	 	$this->_field = $field;
	 	return $this;
	 }
	 
	 /**
	  * 关联操作
	  * @param unknown_type $join
	  */
	 public function join($join)
	 {
	 	if (strpos(strtolower($join), 'join') !== false)
	 		$this->_join[] = $join;
	 	else 
	 		$this->_join[] = ' LEFT JOIN '.$join;
	 	return $this;
	 }
	 
	 /**
	  * 条件操作
	  * @param unknown_type $where
	  */
	 public function where($where)
	 {
		 $this->_where = $where;
	 	return $this;
	 }
	 
	 /**
	  * 分页操作
	  * @param unknown_type $page
	  * @param unknown_type $size
	  */
	 public function limit($page = 0,$size = 20)
	 {
	 	if(is_numeric($page) && is_numeric($size))
	 		$this->_limit = $page.','.$size;
	 	return $this;
	 }
	 
	 /**
	  * 分组操作
	  * @param unknown_type $group
	  */
	 public function group($group)
	 {
	 	$this->_group = $group;
	 	return $this;
	 }
	 
	 /**
	  * 数据操作
	  * @param unknown_type $data
	  */
	 public function data($data)
	 {
	 	$this->_data = $data;
	 	return $this;
	 }
	 
	 /**
	  * 排序
	  * @param unknown_type $order
	  */
	 public function order($order)
	 {
	 	$this->_order = $order;
	 	return $this;
	 }
	 
	 /**
	  * 查询
	  */
	 public function select()
	 {
	 	$sql = 'SELECT '.$this->_field.' FROM '.$this->_table;
	 	if (isset($this->_join) && $this->_join)
	 		foreach ($this->_join as $one)
	 			$sql .= ' '.$one.' ';
	 	$sql .= isset($this->_where) && $this->_where?' WHERE '.$this->_where:'';
	 	$sql .= isset($this->_group) && $this->_group?' GROUP BY '.$this->_group:'';
	 	$sql .= isset($this->_limit) && $this->_limit?' LIMIT '.$this->_limit:'';
	 	$sql .= isset($this->_order) && $this->_order?' ORDER BY '.$this->_order:'';
	 	$result = $this->_drive->select($sql);
	 	$this->_destroy();
	 	return $result;
	 }
	 
	 /**
	  * 新增
	  */
	 public function insert()
	 {
	 	if(is_array($this->_data))
	 	{
	 		$data = array();
	 		foreach ($this->_data as $key => $value)
	 			$data[] = ' `'.$key.'` = \''.$value.'\' ';
	 		$sql = 'INSERT INTO '.$this->_table.' SET '.implode(',', $data);
	 		$sql .= isset($this->_where) && $this->_where?' WHERE '.$this->_where:'';
	 		$result = $this->_drive->insert($sql);
	 		$this->_destroy();
	 		return $result;
	 	}
	 	return false;
	 }
	 
	 /**
	  * 修改
	  */
	 public function update()
	 {
	 	if(is_array($this->_data))
	 	{
	 		$data = array();
	 		foreach ($this->_data as $key => $value)
	 			$data[] = ' `'.$key.'` = \''.$value.'\' ';
	 		$sql = 'UPDATE '.$this->_table.' SET '.implode(',', $data);
	 		$sql .= isset($this->_where) && $this->_where?' WHERE '.$this->_where:'';
	 		$result = $this->_drive->update($sql);
	 		$this->_destroy();
	 		return $result;
	 	}
	 	return false;
	 }
	 
	 /**
	  * 删除
	  */
	 public function delete()
	 {
	 	$sql = 'DELETE FROM '.$this->_table;
	 	$sql .= isset($this->_where) && $this->_where?' WHERE '.$this->_where:'';
	 	$result = $this->_drive->delete($sql);
	 	$this->_destroy();
	 	return $result;
	 }
	 
	 /**
	  * 总数
	  */
	 public function count()
	 {
	 	$sql = 'SELECT COUNT(*) AS count FROM '.$this->_table;
	 	if (isset($this->_join) && $this->_join)
	 		foreach ($this->_join as $one)
	 			$sql .= ' '.$one.' ';
	 	$sql .= isset($this->_where) && $this->_where?' WHERE '.$this->_where:'';
	 	$sql .= isset($this->_group) && $this->_group?' GROUP BY '.$this->_group:'';
	 	$sql .= isset($this->_limit) && $this->_limit?' LIMIT '.$this->_limit:'';
	 	$result = $this->_drive->count($sql);
	 	$this->_destroy();
	 	return $result;
	 }
	 
	 /**
	  * 直接执行sql
	  * @param unknown_type $sql
	  */
	 public function sql($sql)
	 {
	 	return $this->_drive->sql($sql);
	 }
	 
	 /**
	  * 清除数据
	  */
	 protected function _destroy()
	 {
// 	 	unset($this->_table);
	 	unset($this->_where);
	 	$this->_field = '*';
	 	unset($this->_join);
	 	unset($this->_limit);
	 	unset($this->_group);
	 	unset($this->_data);
	 	unset($this->_order);
	 }
}
?>